package com.blb.controller;


import com.blb.entity.Order;
import com.blb.entity.StayProduction;
import com.blb.service.*;
import com.blb.service.impl.OrderServiceImpl;
import com.blb.utils.Response;
import com.blb.utils.ShiroUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *销售管理的订单部分
 * @author wss
 * @since 2020-05-22
 */
@Controller
@RequestMapping("/order")
public class OrderController {


    @Resource
    private IOrderService orderService;

    @Autowired
    IStayProductionService iStayProductionService;

    @Resource
    IDriverService driverService;

    @Resource
    IVehicleService vehicleService;

    @Resource
    ILogisticsService logisticsService;

    @Resource
    IProductService productService;
    @RequiresPermissions("ordersort")
    @GetMapping("sort")
    public String orderSort(){
        return "order/order_sort";
    }

    @RequiresPermissions("orderlist")
    @GetMapping("dealAuditing")
    public String dealAuditing(){
        return "order/deal_auditing";
    }
    @RequiresPermissions("purchaselist")
    @GetMapping("orderCx")
    public String orderCx(){
        return "order/Order_cx";
    }


    @RequiresPermissions("orderlist")
    @GetMapping("list")
    public String orderList(Map<String,Object> map ){
        map.put("whole",orderService.getCountByOrderStatus(null,ShiroUtils.getCompany()));
        map.put("complete",orderService.getCountByOrderStatus("已完成",ShiroUtils.getCompany()));
        map.put("notSet",orderService.getCountByOrderStatus("未发货",ShiroUtils.getCompany()));
        map.put("notSend",orderService.getCountByOrderStatus("待生成",ShiroUtils.getCompany()));
        map.put("set",orderService.getCountByOrderStatus("已发货",ShiroUtils.getCompany()));

        map.put("products",productService.list());
        return "order/detail_List";
    }

    /**
     * 订单发货页面
     * @param map
     * @return
     */
    @RequiresPermissions("orderfa")
    @GetMapping("aaDF")
    public String orderDF(Map<String,Object> map){
        map.put("drivers",driverService.list());
        map.put("vehicles",vehicleService.list());
        map.put("logistics",logisticsService.list());
        return "order/dd_fh";
    }
    @RequiresPermissions("orderlist")
    @ResponseBody
    @GetMapping("/pages")
    public  Map<String, Object> getPages(@Param("start") Integer start, @Param("length") Integer length , @Param("sldd") String sldd){

        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("iTotalRecords",orderService.getCount(ShiroUtils.getCompany()));
        pageMap.put("iTotalDisplayRecords",orderService.getCount(ShiroUtils.getCompany()));
        pageMap.put("data",orderService.getPages(start,length,sldd,null,null,null,ShiroUtils.getCompany()));
        return pageMap;
    }

    @RequiresPermissions("orderlist")
    @ResponseBody
    @GetMapping("/orderAdd")
    public  Map<String,Object> getOrderAdd(@Param("orderId") String orderId ){
        Map<String, Object> map = new HashMap<>();
        Order order = orderService.getByOrderID(orderId);
        map.put("order",order);
        return map;
    }
    @RequiresPermissions("orderlist")
    @ResponseBody
    @PostMapping("/add")
    public String add(){

        return "添加成功";
    }

    @RequiresPermissions("orderfa:send")
    @ResponseBody
    @GetMapping("/sendList")
    public  Map<String, Object> getSendList(@Param("start") Integer start, @Param("length") Integer length , @Param("orderStatus") String orderStatus){

        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("iTotalRecords",orderService.getCountByOrderStatus("未发货",ShiroUtils.getCompany()));
        pageMap.put("iTotalDisplayRecords",orderService.getCountByOrderStatus("未发货",ShiroUtils.getCompany()));
        pageMap.put("data",orderService.getPages(start,length,"0","未发货",null,null,ShiroUtils.getCompany()));
        return pageMap;
    }
    //@RequiresPermissions("printer:deleted")
    @GetMapping("/ddToDydd")
    public  String getDdToDydd(@Param("orderId") String orderId,Map<String,Object> map){
        Order orders = orderService.getAll(orderId);
        map.put("orders", orders);
        int num=Integer.parseInt(orders.getSum());
        if(orders.getProduct().getRetailPrice()==null){
            map.put("total", "0");
        }else {
            Float total =orders.getProduct().getRetailPrice()*num;
            map.put("total", total);
        }

        //获取当前时间格式转化
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        String format = simpleDateFormat.format(date);
        map.put("format",format);


        return "order/dd_dydd";
    }

    //@RequiresPermissions("printer:deleted")
    @GetMapping("/ddToDyfhd")
    public  String getDdToDyfhd(@Param("orderId") String orderId,Map<String,Object> map){

        Order orders = orderService.getAll(orderId);
        map.put("orders", orders);
        int num=Integer.parseInt(orders.getSum());
        if(orders.getProduct().getRetailPrice()==null){
            map.put("total", "0");
        }else {
            Float total =orders.getProduct().getRetailPrice()*num;
            map.put("total", total);
        }

        //获取当前时间格式转化
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        String format = simpleDateFormat.format(date);
        map.put("format",format);

        return "order/dd_dyfhd";
    }
    @RequiresPermissions("orderlist")
    @Transactional
    @ResponseBody
    @GetMapping("/arrangement")
    public Response getArrangement(@RequestParam("orderId")String orderId,@RequestParam("sldd")String sldd,
    @RequestParam("productPlanNo")String productPlanNo ,@RequestParam("productPlanTime")String productPlanTime){

        System.out.println(orderId);
        System.out.println(sldd);

        StayProduction stayProduction = new StayProduction();
        stayProduction.setIoId(orderId);
        stayProduction.setProductionName(sldd);
        stayProduction.setState(0);
        stayProduction.setCompanyId(ShiroUtils.getCompany());
        stayProduction.setId(orderId);
        stayProduction.setLState("待生成");

        //把订单状态改为待生成
        Order order = new Order();
        Order byOrderID = orderService.getByOrderID(orderId);
        order.setId(byOrderID.getId());
        order.setOrderStatus("待生成");
        orderService.updateById(order);

        iStayProductionService.save(stayProduction);
        return Response.success();
    }

//    @ResponseBody
//    @GetMapping("/deliver")
//    public Response getDeliver(String orderId){
//
//        System.out.println(orderId);
//        return Response.success();
//    }
    @RequiresPermissions("orderlist")
    @ResponseBody
    @GetMapping("/orderList")
    public  Map<String, Object> getOrderList(@Param("start") Integer start, @Param("length") Integer length, @Param("sldd") String sldd ,
                                             @Param("orderStatus") String orderStatus,@Param("orderId")String orderId ,@Param("orderTime")String orderTime){
        System.out.println(orderId);
        System.out.println(start);
        System.out.println(length);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("iTotalRecords",orderService.getCount(ShiroUtils.getCompany()));
        pageMap.put("iTotalDisplayRecords",orderService.getCount(ShiroUtils.getCompany()));
        pageMap.put("data",orderService.getPages(start,length,sldd,orderStatus,orderId,orderTime,ShiroUtils.getCompany()));
        return pageMap;
    }

    @RequiresPermissions("orderlist")
    @Transactional
    @ResponseBody
    @RequestMapping("addOrderList")
    public Response addOrderList(@RequestBody(required=false) Order order){
        System.out.println(order);
        order.setId(order.getOrderId());
        order.setCompanyId(ShiroUtils.getCompany());
        order.setProId(order.getProId());
        order.setCuId(ShiroUtils.getCompany());
        order.setOrderStatus("未排单");
//        Date currentTime = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String dateString = formatter.format(currentTime);
//        ParsePosition pos = new ParsePosition(8);
//        Date currentTime_2 = formatter.parse(dateString, pos);
//        order.setOrderTime(currentTime_2);
        orderService.save(order);
        return Response.success();
    }




}

